package com.actions.bluetoothbox2.fragment;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.bluetoothbox2.app.MyApplication;
import com.actions.bluetoothbox2.util.ActionAlarmInfo;
import com.actions.bluetoothbox2.util.DButils;
import com.actions.bluetoothbox2.util.PopupColorSelecter;
import com.actions.bluetoothbox2.util.PopupColorSelecter2;
import com.actions.bluetoothbox2.util.PopupVoiceSelecter;
import com.actions.bluetoothbox2.util.PopupVoiceSelecter2;
import com.actions.bluetoothbox2.util.Utils;
import com.actions.bluetoothbox2.util.VoiceInfo;
import com.actions.bluetoothbox2.widget.AlarmClockTimePicker;
import com.actions.ibluz.manager.BluzManager;
import com.actions.ibluz.manager.BluzManagerData;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddActionFragment extends Fragment {

    private EditText add_actong_edt;
    private TextView add_stop_time, tv_volume;
    private SeekBar seekbar_stop_time, seekbar_volume;
    private Button btn_music_select, btn_lighting_select, btn_lighting_s, btn_ok;
    private BrowserActivity mActivity;
    private BluzManager mBluzManager;
    private ImageView[] actongImg = new ImageView[9];
    private CheckBox[] mRepeatButtons = new CheckBox[7];
    private AlarmClockTimePicker mTimePicker;
    private int[] actions = new int[]{R.drawable.ic_action_1, R.drawable.ic_action_2, R.drawable.ic_action_3,
            R.drawable.ic_action_4, R.drawable.ic_action_5, R.drawable.ic_action_6, R.drawable.ic_action_7,
            R.drawable.ic_action_8, R.drawable.ic_action_9};
    private int[] action_selects = new int[]{R.drawable.ic_action_select_1, R.drawable.ic_action_select_2, R.drawable.ic_action_select_3,
            R.drawable.ic_action_select_4, R.drawable.ic_action_select_5, R.drawable.ic_action_select_6, R.drawable.ic_action_select_7,
            R.drawable.ic_action_select_8, R.drawable.ic_action_select_9};

    private int voice_imgID[] = new int[]{R.drawable.ic_voice_1, R.drawable.ic_voice_2, R.drawable.ic_voice_3, R.drawable.ic_voice_4,
            R.drawable.ic_voice_5, R.drawable.ic_voice_6, R.drawable.ic_voice_7, R.drawable.ic_voice_8, R.drawable.ic_voice_9,
            R.drawable.ic_voice_10, R.drawable.ic_voice_11, R.drawable.ic_voice_12, R.drawable.ic_voice_13, R.drawable.ic_voice_14,
            R.drawable.ic_voice_15};
    private int led_imgID[] = new int[]{R.drawable.ic_color1, R.drawable.ic_color2, R.drawable.ic_color3, R.drawable.ic_color4,
            R.drawable.ic_color5, R.drawable.ic_color6, R.drawable.ic_color7, R.drawable.ic_color8, R.drawable.ic_color9,
            R.drawable.ic_color10, R.drawable.ic_color11, R.drawable.ic_color12, R.drawable.ic_color13, R.drawable.ic_color14,
            R.drawable.ic_color15};

    private PopupVoiceSelecter popupVoiceSelecter;
    private PopupVoiceSelecter2 popupVoiceSelecter2;
    private PopupColorSelecter popupColorSelecter;
    private PopupColorSelecter2 popupColorSelecter2;

    //    private int voice_id;//音频id
//    private VoiceInfo voice_info;//音频
//    private int lighting_id;//灯光id
//    private int lighting_color;
    private ActionAlarmInfo info;
    private List<VoiceInfo> list_voice;

    public AddActionFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add_action, container, false);
        mActivity = (BrowserActivity) getActivity();
        mBluzManager = mActivity.getBluzManager();

        mActivity.tv_title.setText(getResources().getText(R.string.add_acting));
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.title_back.setVisibility(View.VISIBLE);
        mActivity.mGlobalInfoLayout.setVisibility(View.GONE);

        mActivity.title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switchFragment();
            }
        });

        InitView(view);

        Bundle bundle = getArguments();
        if (bundle != null) {
            info = (ActionAlarmInfo) bundle.getSerializable("Second");
            setData();
        }
        return view;
    }

    private void InitView(View view) {
        add_actong_edt = (EditText) view.findViewById(R.id.add_actong_edt);
        actongImg[0] = (ImageView) view.findViewById(R.id.add_grid_img2);
        actongImg[1] = (ImageView) view.findViewById(R.id.add_grid_img3);
        actongImg[2] = (ImageView) view.findViewById(R.id.add_grid_img4);
        actongImg[3] = (ImageView) view.findViewById(R.id.add_grid_img5);
        actongImg[4] = (ImageView) view.findViewById(R.id.add_grid_img6);
        actongImg[5] = (ImageView) view.findViewById(R.id.add_grid_img7);
        actongImg[6] = (ImageView) view.findViewById(R.id.add_grid_img8);
        actongImg[7] = (ImageView) view.findViewById(R.id.add_grid_img9);
        actongImg[8] = (ImageView) view.findViewById(R.id.add_grid_img10);
//        actongImg[9] = (ImageView) view.findViewById(R.id.add_grid_img10);
//        add_start_time = (TextView) view.findViewById(R.id.add_start_time);
        add_stop_time = (TextView) view.findViewById(R.id.add_stop_time);
        tv_volume = (TextView) view.findViewById(R.id.tv_volume);

        mRepeatButtons[0] = (CheckBox) view.findViewById(R.id.Sunday);
        mRepeatButtons[1] = (CheckBox) view.findViewById(R.id.Monday);
        mRepeatButtons[2] = (CheckBox) view.findViewById(R.id.Tuesday);
        mRepeatButtons[3] = (CheckBox) view.findViewById(R.id.Wednesday);
        mRepeatButtons[4] = (CheckBox) view.findViewById(R.id.Thursday);
        mRepeatButtons[5] = (CheckBox) view.findViewById(R.id.Friday);
        mRepeatButtons[6] = (CheckBox) view.findViewById(R.id.Saturday);

        mTimePicker = (AlarmClockTimePicker) view.findViewById(R.id.alarmclock_time);
        mTimePicker.setSaveFromParentEnabled(false);
        mTimePicker.setSaveEnabled(true);

        seekbar_stop_time = (SeekBar) view.findViewById(R.id.seekbar_stop_time);
        seekbar_volume = (SeekBar) view.findViewById(R.id.seekbar_volume);
        seekbar_stop_time.setOnSeekBarChangeListener(listener_seekbar);
        seekbar_volume.setOnSeekBarChangeListener(listener_seekbar);

        btn_music_select = (Button) view.findViewById(R.id.btn_music_select);
        btn_lighting_select = (Button) view.findViewById(R.id.btn_lighting_select);
        btn_lighting_s = (Button) view.findViewById(R.id.btn_lighting_s);
        btn_ok = (Button) view.findViewById(R.id.btn_ok);

        popupVoiceSelecter2 = new PopupVoiceSelecter2(mActivity);
        popupVoiceSelecter2.setSelected(selecte_voice2);
        popupVoiceSelecter = new PopupVoiceSelecter(mActivity, popupVoiceSelecter2, btn_music_select);
        popupVoiceSelecter.setSelected(selecte_voice);

        popupColorSelecter2 = new PopupColorSelecter2(mActivity);
        popupColorSelecter2.setSelected(onselecte_color1);
        popupColorSelecter = new PopupColorSelecter(mActivity, popupColorSelecter2, btn_lighting_select);
        popupColorSelecter.setSelected(onSelecte_color);

        if(mActivity.is_TF){
            list_voice =DButils.queryAll();
            if (null == list_voice || list_voice.size() == 0) {
                getVoide();
            } else {
                IsUpdata();
            }
        }

        btn_music_select.setOnClickListener(click);
        btn_lighting_select.setOnClickListener(click);
        btn_lighting_s.setOnClickListener(click);
        btn_ok.setOnClickListener(click);

    }

    SeekBar.OnSeekBarChangeListener listener_seekbar = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (seekBar.getId() == R.id.seekbar_stop_time) {
                add_stop_time.setText("" + i);
            } else if (seekBar.getId() == R.id.seekbar_volume) {
                tv_volume.setText(i + "");
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };
    PopupVoiceSelecter.onSelecteListener selecte_voice = new PopupVoiceSelecter.onSelecteListener() {
        @Override
        public void onSelected(int position, int imgID) {
            //音频id
//            voice_id = position + 1;
            btn_music_select.setBackgroundResource(imgID);
            info.ring_seq = position + 1;
            info.alarm_type = 3;
        }
    };
    PopupVoiceSelecter2.onSelecteListener selecte_voice2 = new PopupVoiceSelecter2.onSelecteListener() {
        @Override
        public void onSelected(VoiceInfo infov) {
//            voice_info = info;
            btn_music_select.setBackgroundResource(R.drawable.ic_voice_15);
//            voice_id = 15;
            info.song_name_b = infov.song_name_b;
            info.ring_seq = infov.song_seq;
            info.alarm_type = 1;
        }
    };
    PopupColorSelecter2.onSelecteListener onselecte_color1 = new PopupColorSelecter2.onSelecteListener() {
        @Override
        public void onSelected(int color) {
            //灯光颜色
//            lighting_id = 15;
//            lighting_color = color;

            btn_lighting_select.setVisibility(View.GONE);
            btn_lighting_s.setVisibility(View.VISIBLE);
            GradientDrawable myGrad = (GradientDrawable) btn_lighting_s.getBackground();
            myGrad.setColor(color);

            info.led_action = 0;
            int red = (color & 0xff0000) >> 16;
            int green = (color & 0x00ff00) >> 8;
            int blue = (color & 0x0000ff);
            info.led_R = red;
            info.led_G = green;
            info.led_B = blue;
        }
    };
    PopupColorSelecter.onSelecteListener onSelecte_color = new PopupColorSelecter.onSelecteListener() {
        @Override
        public void onSelected(int position, int imgID) {
            if (position == 14) {
            } else {
                btn_lighting_select.setVisibility(View.VISIBLE);
                btn_lighting_s.setVisibility(View.GONE);
                //灯光颜色id
                btn_lighting_select.setBackgroundResource(imgID);
//                lighting_id = position + 1;
//                info.led_action = position + 1;

                switch (position + 1) {
                    case 1:
                        info.led_action = 0x80;
                        break;
                    case 2:
                        info.led_action = 0x81;
                        break;
                    case 3:
                        info.led_action = 0x82;
                        break;
                    case 4:
                        info.led_action = 0x83;
                        break;
                    case 5:
                        info.led_action = 0x84;
                        break;
                    case 6:
                        info.led_action = 0x85;
                        break;
                    case 7:
                        info.led_action = 0x86;
                        break;
                    case 8:
                        info.led_action = 0x87;
                        break;
                    case 9:
                        info.led_action = 0x88;
                        break;
                    case 10:
                        info.led_action = 0x89;
                        break;
                    case 11:
                        info.led_action = 1;
                        break;
                    case 12:
                        info.led_action = 2;
                        break;
                    case 13:
                        info.led_action = 3;
                        break;
                    case 14:
                        info.led_action = 4;
                        break;
                    default:
                        break;

                }
            }
        }
    };

    View.OnClickListener click = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_music_select:
                    popupVoiceSelecter.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    break;
                case R.id.btn_lighting_select:
                    popupColorSelecter.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    break;
                case R.id.btn_lighting_s:

                    popupColorSelecter.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    break;
                case R.id.btn_ok:
                    SendData();

//                    mActivity.backFragment(new TimeTableFragment(), SlideLeftFragment.FRAGMENT_TAG_TIMETABLE);
                    break;
                default:
                    break;
            }
        }
    };

    private void switchFragment() {
        TimeTableFragment fragment = new TimeTableFragment();
        mActivity.replaceFragment(fragment, SlideLeftFragment.FRAGMENT_TAG_TIMETABLE);
    }

    private void IsUpdata() {
        mBluzManager.setOnCustomCommandListener(new BluzManagerData.OnCustomCommandListener() {

            @Override
            public void onReady(int i, int i1, int i2, byte[] bytes) {
                int type = Utils.bytesToInt(bytes, 0);
                Log.i("TAG", "onReady: ----type == " + type);
                if (i1 == 1 && type == 1) {
                    getVoide();
                } else {
                    popupVoiceSelecter2.setList_voice(list_voice);
                }
            }
        });
        int cmd_type = Integer.decode("0x51");
        int cmd_id = Integer.decode("0x81");
        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 1, 0, null);
    }

    public void getVoide() {
        mBluzManager.setOnCustomCommandListener(new BluzManagerData.OnCustomCommandListener() {
            @Override
            public void onReady(int i, int i1, int i2, byte[] bytes) {

                DButils.deleteLove();
                List<VoiceInfo> infos_music = new ArrayList<>();
                List<Byte> list_byte = new ArrayList<>();
                for (int j = 0; j < bytes.length; j++) {
                    list_byte.add(bytes[j]);
                }

                int count = bytes.length / 64;
                for (int j = 0; j < count; j++) {
                    List<Byte> byet_list = list_byte.subList(j * 64, 64 * (j + 1));

                    VoiceInfo info = new VoiceInfo();
                    info.ring_type = byet_list.get(0);

                    byte[] b1 = new byte[4];
                    b1[0] = byet_list.get(1);
                    b1[1] = byet_list.get(2);
                    b1[2] = 0;
                    b1[3] = 0;
                    info.song_seq = Utils.bytesToInt(b1, 0);

                    info.code_type = byet_list.get(3);

                    byte b2[] = new byte[60];
                    for (int l = 0; l < 60; l++) {
                        b2[l] = byet_list.get(l + 4);
                    }
                    info.song_name_b = b2;
                    try {
                        int coun = Utils.getVirtualValueLength(b2, info.ring_type);
                        byte[] b3 = new byte[coun];
                        for (int k = 0; k < coun; k++) {
                            b3[k] = b2[k];
                        }
                        switch (info.ring_type) {
                            case 0:
                                info.song_name = new String(b3, "GBK");
                                break;
                            case 1:
                                info.song_name = new String(b3, "UTF-16LE");
                                break;
                            case 2:
                                info.song_name = new String(b3, "UTF-16BE");
                                break;
                            case 3:
                                info.song_name = new String(b3, "UTF-8");
                                break;
                            default:
                                break;
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    DButils.insertCart(info);
                    infos_music.add(info);
                }
                popupVoiceSelecter2.setList_voice(infos_music);
            }
        });
        int cmd_type = Integer.decode("0x51");
        int cmd_id = Integer.decode("0x99");
        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, null);
    }

    public void SendData() {

        Log.i("TAG", "SendData: ----alarm_type==" + info.alarm_type + "---ring_seq=="
                + info.ring_seq + "---song_name==" + info.song_name + "---led_action=="
                + info.led_action + "---led_R==" + info.led_R + "---led_G==" + info.led_G + "---led_B==" + info.led_B);

        byte[] bytes = new byte[84];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = 0;
        }

        bytes[0] = (byte) info.alarm_id;
        bytes[1] = 1;
        bytes[2] = 1;
        byte Day = 0x00;
        if (mRepeatButtons[0].isChecked()) {
            Day |= 0x01;
        }
        if (mRepeatButtons[1].isChecked()) {
            Day |= 0x02;
        }
        if (mRepeatButtons[2].isChecked()) {
            Day |= 0x04;
        }
        if (mRepeatButtons[3].isChecked()) {
            Day |= 0x08;
        }
        if (mRepeatButtons[4].isChecked()) {
            Day |= 0x10;
        }
        if (mRepeatButtons[5].isChecked()) {
            Day |= 0x20;
        }
        if (mRepeatButtons[6].isChecked()) {
            Day |= 0x40;
        }
        bytes[3] = Day;

        int h = mTimePicker.getCurrentHour();
        bytes[4] = (byte) h;
        int m = mTimePicker.getCurrentMinute();
        bytes[5] = (byte) m;
        bytes[6] = 0;
        if (info.alarm_type == 1) {
            bytes[7] = 1;
            if (null == info.song_name_b) {
                Toast.makeText(mActivity, "请选择铃声", Toast.LENGTH_SHORT).show();
                return;
            } else {
                for (int i = 0; i < 32; i++) {
                    bytes[i + 52] = info.song_name_b[i];
                }
                byte[] bytes1 = Utils.intToBytes(info.ring_seq);
                bytes[12] = bytes1[0];
                bytes[13] = bytes1[1];
            }
        } else if (info.alarm_type == 3) {
            if (info.ring_seq == 0) {
                Toast.makeText(mActivity, "请选择铃声", Toast.LENGTH_SHORT).show();
                return;
            }
            bytes[7] = 3;
            byte[] bytes1 = Utils.intToBytes(info.ring_seq);
            bytes[12] = bytes1[0];
            bytes[13] = bytes1[1];
        }

        if (info.led_action == 0) {
            if (info.led_R == 0 && info.led_G == 0 && info.led_B == 0) {
                Toast.makeText(mActivity, "请选择灯光颜色", Toast.LENGTH_SHORT).show();
                return;
            }
            bytes[8] = 0;
//            int red = (lighting_color & 0xff0000) >> 16;
//            int green = (lighting_color & 0x00ff00) >> 8;
//            int blue = (lighting_color & 0x0000ff);
            bytes[9] = (byte) info.led_R;
            bytes[10] = (byte) info.led_G;
            bytes[11] = (byte) info.led_B;
        } else {
            bytes[8] = (byte) info.led_action;
            switch ( info.led_action ){
                case 0x80:
                    bytes[9] = 0;
                    bytes[10] = 0;
                    bytes[11] = 0;
                    break;
                case 0x81:
                    bytes[9] = (byte) 0xff;
                    bytes[10] = (byte) 0xff;
                    bytes[11] = (byte) 0xff;
                    break;
                case 0x82:
                    bytes[9] = (byte) 0xff;
                    bytes[10] = (byte) 0x0f;
                    bytes[11] = (byte) 0x05;
                    break;
                case 0x83:
                    bytes[9] = (byte) 0xff;
                    bytes[10] = (byte) 0x57;
                    bytes[11] = (byte) 0x00;
                    break;
                case 0x84:
                    bytes[9] = (byte) 0xff;
                    bytes[10] = (byte) 0x8f;
                    bytes[11] = (byte) 0x00;
                    break;
                case 0x85:
                    bytes[9] = (byte) 0x55;
                    bytes[10] = (byte) 0xd6;
                    bytes[11] = (byte) 0x40;
                    break;
                case 0x86:
                    bytes[9] = (byte) 0x5c;
                    bytes[10] = (byte) 0xd1;
                    bytes[11] = (byte) 0xff;
                    break;
                case 0x87:
                    bytes[9] = (byte) 0x1d;
                    bytes[10] = (byte) 0x10;
                    bytes[11] = (byte) 0xe5;
                    break;
                case 0x88:
                    bytes[9] = (byte) 0xd1;
                    bytes[10] = (byte) 0x00;
                    bytes[11] = (byte) 0xf9;
                    break;
                case 0x89:
                    bytes[9] = (byte) 0xff;
                    bytes[10] = (byte) 0x0d;
                    bytes[11] = (byte) 0x85;
                    break;
                default:
                    break;
            }

        }

        int endTime = Integer.parseInt(add_stop_time.getText().toString());
        byte[] bytes2 = Utils.intToBytes(endTime);
        bytes[14] = bytes2[0];
        bytes[15] = bytes2[1];

        int volume = Integer.parseInt(tv_volume.getText().toString());
        bytes[16] = (byte) volume;
        bytes[17] = 0;
        bytes[18] = 0;
        bytes[19] = 0;

        byte[] b_title = add_actong_edt.getText().toString().getBytes();
        if (b_title.length < 32) {
            for (int i = 0; i < b_title.length; i++) {
                bytes[i + 20] = b_title[i];
            }
        } else {
            for (int i = 0; i < 32; i++) {
                bytes[i + 20] = b_title[i];
            }
        }
        int cmd_type = Integer.decode("0x53");
        int cmd_id = Integer.decode("0x9b");
        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, bytes);
        switchFragment();
    }

    private void setData() {
        Log.i("TAG", "setData: ----alarm_type==" + info.alarm_type + "---ring_seq=="
                + info.ring_seq + "---song_name==" + info.song_name + "---led_action=="
                + info.led_action + "---led_R==" + info.led_R + "---led_G==" + info.led_G + "---led_B==" + info.led_B);
        for (int i = 0; i < actions.length; i++) {
            if (i < 9) {
                if ((info.alarm_id - 1) == i) {
                    actongImg[i].setImageResource(action_selects[i]);
                } else {
                    actongImg[i].setImageResource(actions[i]);
                }
            } else {
                actongImg[i].setImageResource(action_selects[8]);
            }
        }

        if (info.alarm_day == 0x7f) {
//每天
            for (int i = 0; i < mRepeatButtons.length; i++) {
                mRepeatButtons[i].setChecked(true);
            }
        } else {
            if ((info.alarm_day & 0x01) == 0x01) {
//星期天
                mRepeatButtons[0].setChecked(true);
            }
            if ((info.alarm_day & 0x02) == 0x02) {
//星期一
                mRepeatButtons[1].setChecked(true);
            }
            if ((info.alarm_day & 0x04) == 0x04) {
//星期二
                mRepeatButtons[2].setChecked(true);
            }
            if ((info.alarm_day & 0x08) == 0x08) {
//星期三
                mRepeatButtons[3].setChecked(true);
            }
            if ((info.alarm_day & 0x10) == 0x10) {
//星期四
                mRepeatButtons[4].setChecked(true);
            }
            if ((info.alarm_day & 0x20) == 0x20) {
//星期五
                mRepeatButtons[5].setChecked(true);
            }
            if ((info.alarm_day & 0x40) == 0x40) {
//星期六
                mRepeatButtons[6].setChecked(true);
            }
        }

        add_actong_edt.setText(info.name);
        if (info.alarm_hour != 0) {
            mTimePicker.setCurrentHour(info.alarm_hour);
            mTimePicker.setCurrentMinute(info.alarm_m);
        }
        add_stop_time.setText(info.ring_endtime + "");
        seekbar_stop_time.setProgress(info.ring_endtime);
        tv_volume.setText(info.led_volume + "");
        seekbar_volume.setProgress(info.led_volume);

        if (info.alarm_type == 3) {
            btn_music_select.setBackgroundResource(voice_imgID[info.ring_seq - 1]);
//            voice_id = info.ring_seq;
        } else if (info.alarm_type == 0) {

        } else {
            btn_music_select.setBackgroundResource(voice_imgID[14]);
        }

        if (info.led_action == 0) {
            if (info.led_R == 0 && info.led_G == 0 && info.led_B == 0) {

            } else {
                GradientDrawable myGrad = (GradientDrawable) btn_lighting_s.getBackground();
                myGrad.setColor(Color.rgb(info.led_R, info.led_G, info.led_B));

                btn_lighting_select.setVisibility(View.GONE);
                btn_lighting_s.setVisibility(View.VISIBLE);
            }
        } else {
            switch (info.led_action) {
                case 0x80:
//                    btn_lighting_select.setBackgroundResource(led_imgID[0]);
                    break;
                case 0x81:
                    btn_lighting_select.setBackgroundResource(led_imgID[1]);
                    break;
                case 0x82:
                    btn_lighting_select.setBackgroundResource(led_imgID[2]);
                    break;
                case 0x83:
                    btn_lighting_select.setBackgroundResource(led_imgID[3]);
                    break;
                case 0x84:
                    btn_lighting_select.setBackgroundResource(led_imgID[4]);
                    break;
                case 0x85:
                    btn_lighting_select.setBackgroundResource(led_imgID[5]);
                    break;
                case 0x86:
                    btn_lighting_select.setBackgroundResource(led_imgID[6]);
                    break;
                case 0x87:
                    btn_lighting_select.setBackgroundResource(led_imgID[7]);
                    break;
                case 0x88:
                    btn_lighting_select.setBackgroundResource(led_imgID[8]);
                    break;
                case 0x89:
                    btn_lighting_select.setBackgroundResource(led_imgID[9]);
                    break;
                case 1:
                    btn_lighting_select.setBackgroundResource(led_imgID[10]);
                    break;
                case 2:
                    btn_lighting_select.setBackgroundResource(led_imgID[11]);
                    break;
                case 3:
                    btn_lighting_select.setBackgroundResource(led_imgID[12]);
                    break;
                case 4:
                    btn_lighting_select.setBackgroundResource(led_imgID[13]);
                    break;
                default:
                    break;
            }

            btn_lighting_select.setVisibility(View.VISIBLE);
            btn_lighting_s.setVisibility(View.GONE);
        }
    }

}
